package com.sctvcloud.renhe.ui.activities;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.util.ArrayMap;
import android.util.Pair;
import android.view.View;
import android.widget.Toast;

import com.gridsum.tracker.GridsumWebDissector;
import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.dialog.BaseDialogFragment;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.renhe.R;
import com.sctvcloud.renhe.base.BaseActivity;
import com.sctvcloud.renhe.beans.MessageListVo;
import com.sctvcloud.renhe.beans.MessageStateBean;
import com.sctvcloud.renhe.beans.SingleResult;
import com.sctvcloud.renhe.http.AbsNetCallBack;
import com.sctvcloud.renhe.http.NetUtils;
import com.sctvcloud.renhe.ui.dialog.ConfirmDiaFragment;
import com.sctvcloud.renhe.ui.dialog.ShowDiaFragment;
import com.sctvcloud.renhe.ui.utils.TitleUtils;
import com.sctvcloud.renhe.utils.UserManager;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MessageSettingActivity extends BaseActivity {
    public static final String SHOW_READ_TAG = "dia_Read_fragment";
    public static final String SHOW_CLEAN_TAG = "dia_Clean_fragment";
    @BindView(R.id.message_read)
    protected CustomFontTextView messageRead;
    @BindView(R.id.message_clean)
    protected CustomFontTextView messageClean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TitleUtils(this).useAndSetTxTitle("消息设置");
        requestMessageData();
        getMessageData();
    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_message_setting);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.message_read, R.id.message_clean})
    protected void onclick(View view) {
        switch (view.getId()) {
            case R.id.message_read:
                ConfirmDiaFragment readDiaFragment = new ConfirmDiaFragment("请确认消息状态全部改为已读吗？", 1);
                readDiaFragment.setCallBack(callBack);
                readDiaFragment.show(MessageSettingActivity.this.getSupportFragmentManager(), SHOW_READ_TAG);
                break;
            case R.id.message_clean:
                ConfirmDiaFragment cleanDiaFragment = new ConfirmDiaFragment("请确认要清空所有消息吗？", 2);
                cleanDiaFragment.setCallBack(callBack);
                cleanDiaFragment.show(MessageSettingActivity.this.getSupportFragmentManager(), SHOW_CLEAN_TAG);
                break;
        }
    }

    //Dialog点击回调
    public BaseDialogFragment.ICallBack callBack = new BaseDialogFragment.ICallBack() {
        @Override
        public void onCallBackMsg(BaseDialogFragment fragment, Message msg) {
            ArrayList<Pair<String, String>> pairs = new ArrayList<>();
            pairs.add(new Pair<>("页面", "消息设置"));
            if (msg.what == ConfirmDiaFragment.READ_OK) {
                pairs.add(new Pair<>("行为类型", "点击"));
                GridsumWebDissector.getInstance().trackEvent(MessageSettingActivity.this, "", "消息全部已读", "", 200, pairs);
                setReadAll();
            } else if (msg.what == ConfirmDiaFragment.CLEAN_OK) {
                pairs.add(new Pair<>("行为类型", "点击"));
                GridsumWebDissector.getInstance().trackEvent(MessageSettingActivity.this, "", "消息清空", "", 200, pairs);
                cleanAllMsg();
            }
        }
    };

    /**
     * 设置消息全部已读
     */
    private void setReadAll() {
        //TODO 调用接口设置全部已读
        Toast.makeText(MessageSettingActivity.this, "全部已读", Toast.LENGTH_LONG).show();
        ArrayMap<String, Object> p = new ArrayMap<>();
        p.put("msgId", -1);//msgId 为-1是设置全部已读
        p.put("userId", UserManager.getInstance().getUser().getPhoneNumber());
        p.put("token", UserManager.getInstance().getUser().getToken());
        NetUtils.getNetAdapter().postSetReadMessage(getOwnerName(), p, new AbsNetCallBack<SingleResult>(SingleResult.class) {
            @Override
            public void onSuc(SingleResult result) {
                if (result.isSuccess()) {
                    setResult(RESULT_OK);
                    finish();
                }
            }

            @Override
            public void onEnd() {
                super.onEnd();

            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                super.onError(ex, errorMsg);
            }
        });
    }

    /**
     * 设置清空消息
     */
    private void cleanAllMsg() {
        //TODO 调用接口设置清空消息
        Toast.makeText(MessageSettingActivity.this, "清空", Toast.LENGTH_LONG).show();
        ArrayMap<String, Object> p = new ArrayMap<>();
        p.put("userId", UserManager.getInstance().getUser().getPhoneNumber());
        p.put("token", UserManager.getInstance().getUser().getToken());
        NetUtils.getNetAdapter().postCleanMessage(getOwnerName(), p, new AbsNetCallBack<SingleResult>(SingleResult.class) {
            @Override
            public void onSuc(SingleResult result) {
                if (result.isSuccess()) {
                    setResult(RESULT_OK);
                    finish();
                }
            }

            @Override
            public void onEnd() {
                super.onEnd();

            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                super.onError(ex, errorMsg);
            }
        });

    }

    /**
     * 消息状态，如果未读消息数为0，“全部已读”不可用。
     */
    private void requestMessageData() {
        ArrayMap<String, Object> p = new ArrayMap<>();
        p.put("userId", UserManager.getInstance().getUser().getPhoneNumber());
        p.put("token", UserManager.getInstance().getUser().getToken());
        NetUtils.getNetAdapter().postGetMessageState(getOwnerName(), p, new AbsNetCallBack<MessageStateBean>(MessageStateBean.class) {
            @Override
            public void onSuc(MessageStateBean messageStateBean) {
                JLog.e("MessageData ", "count = " + messageStateBean.getCount());
                if (messageStateBean.getCount() > 0) {
                    messageRead.setTextColor(getResources().getColor(R.color.black_2));
                    messageRead.setEnabled(true);
                } else {
                    messageRead.setTextColor(Color.GRAY);
                    messageRead.setEnabled(false);
                }
            }

            @Override
            public void onEnd() {
                super.onEnd();
            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                super.onError(ex, errorMsg);
            }
        });
    }

    /**
     * 获取消息列表，如果消息列表为空，“清空消息”功能不可用
     */
    private void getMessageData() {
        ArrayMap<String, Object> p = new ArrayMap<>();
        p.put("pageIndex", 1);
        p.put("userId", UserManager.getInstance().getUser().getPhoneNumber());
        p.put("token", UserManager.getInstance().getUser().getToken());
        NetUtils.getNetAdapter().postGetMessageList(getOwnerName(), p, new AbsNetCallBack<MessageListVo>(MessageListVo.class) {
            @Override
            public void onSuc(MessageListVo messageListVo) {

                if (messageListVo.getMsgList().size() > 0) {
                    messageClean.setTextColor(getResources().getColor(R.color.black_2));
                    messageClean.setEnabled(true);
                } else {
                    messageClean.setTextColor(Color.GRAY);
                    messageClean.setEnabled(false);
                }
            }

            @Override
            public void onEnd() {
                super.onEnd();

            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                super.onError(ex, errorMsg);
            }
        });
    }

}
